package com.weilive.platform;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import javax.crypto.Cipher;

import com.weilive.core.utils.Base64Utils;
import com.weilive.core.utils.HexStringUtils;
import com.weilive.core.utils.MD5Utils;
import com.weilive.core.utils.RSAUtils;

public class PlatformSecurity {

	public static final int MAX_DATA_LENGTH = 117;

	private static final String privateKeyString = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMu9KS7dl5YvnSr6zhCH/cA3MhD1PqbguA3vLU6Vjr10liETuiVW4HG5jQ0GtH/CjMlo7qaQlJipZwZ+zqrEDL1Wu3xQ7C1U0ceZzpKn4/TyrmF3HnpFoDkNJAVLdzJGA0yXXI+ZummWUq/XjDXCOKNpTyFH484BaBJld0rsCj+lAgMBAAECgYBwZBxo3xL1ZbTE9fBhDDP+KjSzSCbUthiyjyOmOziv6LW87337hS5xbhOSgQVniTJTFVC5j/55LNJJsgBeEaKYf7+MGeFJfQ0B79BySxPNbWvEMO7erm9EDFlvhoiArxOqZtvJVGpdJ5Gz96/dwcSAYRLbNJmk6aervrfpFD68zQJBAPTerjWq+nEbwHw6mXj65V8xrIFjUlUmDigNDFdAMbkam6HH6d62h0e3iinTkn4TYpn+Jy+M628qhyqR/e/CVmcCQQDU/+Dh3gw7WWpOxGKxmqKP8fxEkhiOp0GMvljsVy8Pv9905WBw/D1TsPPyI6LHuvAPmJtFG00+oXYVWPanEboTAkEA61Wr5gZJpTT/vCTxZLT0hpWN7dgPw6fMSS+pIZ2fa/+nfUdNe+6+eu1pSqN8kdB9nfQFowxzJKahuwcOH0QNlwJAQrvMhFeic3HHSdhjp4twRTMmu4EeaotikSsOBzYli4StCR3Ygm209SSquYGKvZMHJYvFkfhE0kuaCIQv+xYluwJAGuAuFaKYamaeZexPGapCW7pTuqYBFQrCktifqx/a5UHHjcZSjBxq/f1mlAZ2BAnkGG4gHU//oJgpRy5yFyW1bQ==";
	private static final String publicKeyString = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLvSku3ZeWL50q+s4Qh/3ANzIQ9T6m4LgN7y1OlY69dJYhE7olVuBxuY0NBrR/wozJaO6mkJSYqWcGfs6qxAy9Vrt8UOwtVNHHmc6Sp+P08q5hdx56RaA5DSQFS3cyRgNMl1yPmbppllKv14w1wjijaU8hR+POAWgSZXdK7Ao/pQIDAQAB";

	public static String getPublicKey() {
		return publicKeyString;
	}

	private static final Cipher clientDecryptCipher = getCipher(
			publicKeyString, true, false);
	private static final Cipher clientEncryptCipher = getCipher(
			publicKeyString, true, true);
	private static final Cipher serverDecryptCipher = getCipher(
			privateKeyString, false, false);
	private static final Cipher serverEncryptCipher = getCipher(
			privateKeyString, false, true);

	/**
	 * Server Cipher creator
	 */

	private static Cipher getCipher(String key, boolean isPublic,
			boolean isEncrypt) {
		try {
			return RSAUtils.getCipher(Base64Utils.decoder(key), isPublic,
					isEncrypt);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * client decrypt and encrypt
	 */

	public static byte[] clientDecrypt(byte[] data) {
		try {
			return clientDecryptCipher.doFinal(data);
		} catch (Exception e) {
			return null;
		}
	}

	public static byte[] clientEncrypt(byte[] data) {
		try {
			return clientEncryptCipher.doFinal(data);
		} catch (Exception e) {
			return null;
		}
	}

	public static String base64ClientDecrypt(String data) {
		try {
			return new String(clientDecryptCipher.doFinal(Base64Utils
					.decoder(data)));
		} catch (Exception e) {
			return null;
		}
	}

	public static String base64ClientEncrypt(String data) {
		try {
			return Base64Utils.encoder(clientEncryptCipher.doFinal(data
					.getBytes()));
		} catch (Exception e) {
			return null;
		}
	}

	public static String hexStringClientDecrypt(String data) {
		try {
			return new String(clientDecryptCipher.doFinal(HexStringUtils
					.decrypt(data)));
		} catch (Exception e) {
			return null;
		}
	}

	public static String hexStringClientEncrypt(String data) {
		try {
			return HexStringUtils.encrypt(clientEncryptCipher.doFinal(data
					.getBytes()));
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * Server decrypt and encrypt
	 */

	public static byte[] serverDecrypt(byte[] data) {
		try {
			return serverDecryptCipher.doFinal(data);
		} catch (Exception e) {
			return null;
		}
	}

	public static byte[] serverEncrypt(byte[] data) {
		try {
			return serverEncryptCipher.doFinal(data);
		} catch (Exception e) {
			return null;
		}
	}

	public static String base64ServerDecrypt(String data) {
		try {
			return new String(serverDecryptCipher.doFinal(Base64Utils
					.decoder(data)));
		} catch (Exception e) {
			return null;
		}
	}

	public static String base64ServerEncrypt(String data) {
		try {
			return Base64Utils.encoder(serverEncryptCipher.doFinal(data
					.getBytes()));
		} catch (Exception e) {
			return null;
		}
	}

	public static String hexStringServerDecrypt(String data) {
		try {
			return new String(serverDecryptCipher.doFinal(HexStringUtils
					.decrypt(data)));
		} catch (Exception e) {
			return null;
		}
	}

	public static String hexStringServerEncrypt(String data) {
		try {
			return HexStringUtils.encrypt(serverEncryptCipher.doFinal(data
					.getBytes()));
		} catch (Exception e) {
			return null;
		}
	}

	public static void main(String[] args) throws Exception {
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
		keyPairGenerator.initialize(1024);
		KeyPair keyPair = keyPairGenerator.generateKeyPair();
		RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
		RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

		String pubKey = HexStringUtils.encrypt(publicKey.getEncoded());
		String priKey = HexStringUtils.encrypt(privateKey.getEncoded());
		System.out.println("publicKey=" + pubKey);
		System.out.println("privateKey=" + priKey);
		
		System.out.println(MD5Utils.encryptToHex("1001爱闹上海微观网络科技有限公司"));
	}
}
